package org.budo.mail.javax.internet.util;

import javax.mail.internet.MimeUtility;

import org.budo.support.lang.util.StringUtil;
import org.budo.support.slf4j.Slf4j;
import org.slf4j.Logger;

/**
 * @author lmw
 */
public class MimeUtil {
    private static final Logger log = Slf4j.getLogger();

    /**
     * 编码
     */
    public static String encodeText(String text) {
        if (StringUtil.isEmpty(text)) {
            return text;
        }

        try {
            return MimeUtility.encodeText(text);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 解码
     */
    public static String decodeText(String text) {
        if (StringUtil.isEmpty(text)) {
            return text;
        }

        try {
            return MimeUtility.decodeText(text);
        } catch (Throwable e) {
            if ((e + "").contains("UnsupportedEncodingException: x-unknown")) {
                log.error("#44 decodeText UnsupportedEncodingException, e=" + e);
                return text;
            }

            throw new RuntimeException(e);
        }
    }
}